
package com.hk.developmentlib.utills;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.webkit.WebSettings;
import android.widget.Toast;

import com.hk.developmentlib.base.DevelopmentKit;

/**
 * 网络情况工具类
 *
 * @author wl
 */
public class NetworkInfoUtils {

    private ConnectivityManager connectMgr;
    private NetworkInfo info;

    public NetworkInfoUtils(Context context) {
        connectMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        info = connectMgr.getActiveNetworkInfo();
    }

    /**
     * 获取网络类型，-1表示没网络，0表示移动数据，1表示wifi
     *
     * @return
     */
    public int getNetType() {
        int netType = -1;
        if (info == null) {
            return netType;
        }
        return info.getType();
    }

    /**
     * 根据当前网路情况获取缓存时间
     *
     * @return
     */
    public static long getCacheTime(Context context) {
        long interval = 0;
        switch (new NetworkInfoUtils(context).getNetType()) {
            case -1:
                Toast.makeText(DevelopmentKit.getContext(), "网络链接已断开，请重试", Toast.LENGTH_SHORT).show();
                break;
            case 0:
                interval = CacheUtils.TIME_LIST_GSM;
                break;
            case 1:
                interval = CacheUtils.TIME_LIST_WIFI;
                break;
        }
        return interval;
    }

    /**
     * 根据当前网路情况设置webview缓存模式
     *
     * @return
     */
    public static int getWebCacheMode(Context context) {
        int mode = 0;
        switch (new NetworkInfoUtils(context).getNetType()) {
            case -1:
                return WebSettings.LOAD_CACHE_ELSE_NETWORK;

            case 0:
                return WebSettings.LOAD_CACHE_ELSE_NETWORK;

            case 1:
                return WebSettings.LOAD_DEFAULT;

        }
        return mode;
    }
}
